package com.flight.trans.util;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.BatteryManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;

import com.flight.trans.R;
import com.flight.trans.base.BaseActivity;
import com.flight.trans.bean.ErrorRouteBean;
import com.flight.trans.global.Constant;
import com.flight.trans.global.ConstantInfo;
import com.flight.trans.global.MainApplication;
import com.flight.trans.listener.OnFinishTouchListener;
import com.flight.trans.util.voice.SpeakUti;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.XXPermissions;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class CommonUtils {

    /**
     * 通过设置全屏，设置状态栏透明
     *
     * @param activity
     */
    public static void changeSystemUi(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    /**
     * 获取当天
     * @return
     */
    public static String getDate(){
        Calendar calendar = Calendar.getInstance();

        return calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取时间字符串
     * @return
     */
    public static String getTime(){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return simpleDateFormat.format(date);
    }


    /**
     * 转换时间
     * @param date
     * @return
     */
    public static String getDate(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }

    /**
     * 转换时间
     * @param date
     * @return
     */
    public static String getTime(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        return simpleDateFormat.format(date);
    }

    /**
     * 获取时间戳
     * @return
     */
    public static String getTimeStamp(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMddhhmmssS");
        return simpleDateFormat.format(new Date(System.currentTimeMillis()));
    }

    /**
     * 字符串转时间
     * @param date
     * @return
     */
    public static Calendar dateToCalender(String date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        try {
            calendar.setTime(format.parse(date));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return calendar ;
    }

    /**
     * 字符串转时间
     * @param date
     * @return
     */
    public static Calendar dateToCalenderTime(String date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Calendar calendar = Calendar.getInstance();

        try {
            calendar.setTime(format.parse(date));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return calendar ;
    }

    /**
     * 字符串转时间戳
     * @param timeString
     * @return
     */
    public static long getTimeLong(String timeString){
        if(TextUtils.isEmpty(timeString))
            return 0;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try{
            return sdf.parse(timeString).getTime();
        } catch(ParseException e){
            e.printStackTrace();
            return 0 ;
        }
    }

    /**
     * Day:日期字符串例如  Num:需要减少的天数例如 7
     * @param day
     * @param Num
     * @return
     */
    public static String getDateCut(String day,int Num) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date nowDate = null;
        try {
            nowDate = df.parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //如果需要向后计算日期 -改为+
        Date newDate2 = new Date(nowDate.getTime() - (long)Num * 24 * 60 * 60 * 1000);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateOk = simpleDateFormat.format(newDate2);
        return dateOk;
    }

    /**
     * Day:日期字符串例如 Num:需要减少的天数例如 7
     * @param day
     * @param Num
     * @return
     */
    public static String getDateAdd(String day,int Num) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date nowDate = null;
        try {
            nowDate = df.parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //如果需要向后计算日期 -改为+
        Date newDate2 = new Date(nowDate.getTime() + (long)Num * 24 * 60 * 60 * 1000);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateOk = simpleDateFormat.format(newDate2);
        return dateOk;
    }

    public static String getDateDay(String data){
        return data.split("-")[2];
    }

    public static boolean compareDate(String data){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dateNow = sdf.parse(getDate());
            Date date = sdf.parse(data);
            //判断日期
            if(((date.getTime() - dateNow.getTime()) /  (24 * 60 * 60 * 1000)) == 7) {
                return false ;
            }else{
                return true ;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return true ;
        }
    }

    /**
     * 获取版本号
     *
     * @param context
     * @return
     */
    public static int getVersionCode(Context context) {
        PackageManager manager = context.getPackageManager();
        int code = 0;
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            code = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return code;
    }

    /**
     * 获取版本号
     *
     * @param context
     * @return
     */
    public static String getVersionName(Context context) {
        PackageManager manager = context.getPackageManager();
        String code = "";
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            code = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return code;
    }

    /**
     * 获取权限
     * @param mActivity
     */
    public static void getPermissions(BaseActivity mActivity){
        if(!XXPermissions.isHasPermission(mActivity , ConstantInfo.PermissionList)){
            XXPermissions.with(mActivity).permission(ConstantInfo.PermissionList).request(new OnPermission() {
                @Override
                public void hasPermission(List<String> granted, boolean isAll) {
                    if(isAll) {
                        SpeakUti.getInstance().init(mActivity);
                    }else {
                        ToastUtils.showLongToast(R.string.no_permission);
                    }
                }

                @Override
                public void noPermission(List<String> denied, boolean quick) {
                }
            });
        }else {
            try {
                SpeakUti.getInstance().init(mActivity);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 留取两位数
     * @param num
     * @return
     */
    public static double getDouble(double num){
        DecimalFormat df = new DecimalFormat("#.00");

        return Double.parseDouble(df.format(num));
    }

    /**
     * 留取4位数
     * @param num
     * @return
     */
    public static String getDoubleFour(double num){
        return String.format(Locale.ENGLISH , "%.4f" , num);
    }

    /**
     * 图片转File
     * @param originBitmap
     */
    public static File bitmapChangeFile(Bitmap originBitmap){
        if(originBitmap == null)
            return null ;

        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        Bitmap.Config config = originBitmap.getConfig();
        Bitmap bitmap = Bitmap.createBitmap(originBitmap.getWidth(), originBitmap.getHeight(), config);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawRect(0, 0, originBitmap.getWidth(), originBitmap.getHeight(), paint);
        canvas.drawBitmap(originBitmap, 0, 0, paint);

        //将要保存图片的路径
        String imagePath = "/saveFile.jpg" ;
        File file = new File(MainApplication.getContext().getExternalCacheDir()  ,imagePath);

        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null ;
        }
        return file ;
    }

    /**
     * 双击
     */
    private static long lastClickTime;
    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        if (time - lastClickTime < 500) {
            return true;
        }

        lastClickTime = time;
        return false;
    }

    /**
     * 连续点击6次
     */
    private static int clickPosition = 1;
    public static boolean isSixClick() {
        long time = System.currentTimeMillis();
        if (time - lastClickTime < 500) {
            lastClickTime = time;
            ++ clickPosition  ;
            if(clickPosition == 5) {
                clickPosition = 0 ;
                return true;
            }else {
                return false ;
            }
        }
        clickPosition = 0 ;
        lastClickTime = time;
        return false;
    }


    /**
     * 测试登录
     */
    public static boolean isTestLoginClick() {
        long time = System.currentTimeMillis();
        long timeGap = time - lastClickTime ;
        if(timeGap < 1200){
            if(clickPosition == 1 || clickPosition == 4 || clickPosition == 5){
                if(timeGap > 400){
                    lastClickTime = time;
                    ++ clickPosition  ;
                    return false;
                }else {
                }
            }else {
                lastClickTime = time;
                ++ clickPosition  ;
                if(clickPosition == 9) {
                    clickPosition = 0 ;
                    return true;
                }else {
                    return false ;
                }
            }
        }else {
            clickPosition = 1 ;
            lastClickTime = time;
            return false ;
        }

        clickPosition = 0 ;
        lastClickTime = 0;
        return false;
    }

    /**
     * 获取误差率
     * @param differenceWeight
     * @param weight
     * @return
     */
    public static ErrorRouteBean getErrorRoute(double differenceWeight ,double containerWeight, double weight){
        double errorWight = differenceWeight - containerWeight - weight ;
        double errorRoute = errorWight / (weight == 0 ? 1 : weight) ;

        double absWight = Math.abs(errorWight);
        double absRoute = Math.abs(errorRoute);

        boolean isError ;

        if(weight < 100){
            isError = absRoute <= 0.05 || absWight <= 5 ;
        }else {
            isError = absRoute <= 0.03 && absWight <= 50 ;
        }

        LogUtils.e("计算" , !isError);

        return new ErrorRouteBean(getDouble(errorRoute) , (absRoute == 1 && containerWeight != 0) ? false : !isError);
    }

    public static String getEmptyContent(String content){
        return !TextUtils.isEmpty(content) ? content : "无" ;
    }

    /**
     * 根据屏幕大小设置是否隐藏
     * @param view
     */
    public static void setScreenViewVisible(View view){
        view.setVisibility(ConstantInfo.notNW ? View.VISIBLE : View.GONE);
    }

    /**
     * 右滑退出
     * @param view
     */
    public static void onFinishTouch(BaseActivity mActivity , View view){
        view.setOnTouchListener(new OnFinishTouchListener(mActivity));
    }

    /**
     * 根据屏幕大小设置是否隐藏
     * @param view
     */
    public static void setScreenViewGone(View view){
        view.setVisibility(ConstantInfo.notNW ? View.GONE : View.VISIBLE);
    }

    /**
     * 根据屏幕大小设置是顶部间距
     * @param view
     */
    public static void setScreenContainerPaddingTop(View view){
        view.setPadding(0 , DensityUtils.dip2px(ConstantInfo.notNW ? 20 : 5) , 0 , 0);
    }

    /**
     * 大小写切换
     * @param content
     */
    public static String toLowerCase(String content){
        byte[] data = content.getBytes();

        int end = 0 + data.length;
        int dist = 'A' - 'a';

        for (int i = 0; i < end; i++)
        {
            if (data[i] >= 'a' && data[i] <= 'z')
            {
                data[i] += dist;
            }
        }

        return new String(data);
    }

    public static String getTimeNorm(String time){

        if(TextUtils.isEmpty(time))
            return  "" ;

        String[] times = time.split(" ");
        if(times == null)
            return time ;

        String timeStr = "";

        if(times.length == 1)
            timeStr = times[0];

        if(times.length == 2){
            String[] dates = times[0].split("-");
            String[] hours = times[1].split(":");
            timeStr = hours[0] + hours[1] + "(" + dates[2] + ")";
        }

        return timeStr ;
    }

    public static boolean isNetworkAvailable() {

        ConnectivityManager connectivity = (ConnectivityManager) MainApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivity != null) {

            NetworkInfo info = connectivity.getActiveNetworkInfo();

            if (info != null && info.isConnected()) {
                //这里可以得到网络状态网络类型等网络相关信息
                // 当前网络是连接的
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    // 当前所连接的网络可用
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 获取电量百分比
     * @param mActivity
     * @return
     */
    public static String powerPercentAge(BaseActivity mActivity){
        BatteryManager manager = (BatteryManager) mActivity.getSystemService(mActivity.BATTERY_SERVICE);
        return manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) + "%";
    }

    public static String writeNetStatusFile(String fileContent){

        String filePath = Constant.Net_File_Path + getDate() + ".txt";

        try {
            File dictionaryFile = new File(Constant.Net_File_Path);//这是目录路径
            if (!dictionaryFile.exists()) {
                dictionaryFile.mkdirs();
            }

            File textFile = new File(filePath);//这是文件路径

            if(!textFile.exists()){
                textFile.createNewFile();
            }

            RandomAccessFile raf = new RandomAccessFile(filePath, "rwd");
            raf.seek(textFile.length());

            raf.write((getTime() + " : " + fileContent + "\n").getBytes());
            raf.close();
            return filePath;
        } catch (Exception e) {
            return "";
        }
    }

    public static String writeCrashLog(String fileContent){

        String filePath = Constant.Crash_File_Path + getDate() + ".txt";

        try {
            File dictionaryFile = new File(Constant.Crash_File_Path);//这是目录路径
            if (!dictionaryFile.exists()) {
                dictionaryFile.mkdirs();
            }

            File textFile = new File(filePath);//这是文件路径

            if(!textFile.exists()){
                textFile.createNewFile();
            }

            RandomAccessFile raf = new RandomAccessFile(filePath, "rwd");
            raf.seek(textFile.length());

            raf.write((getTime() + " : " + fileContent + "\n").getBytes());
            raf.close();
            return filePath;
        } catch (Exception e) {
            return "";
        }
    }

    public static void deleteLog(String path){

        File dictionaryFile = new File(path);//这是目录路径

        if (!dictionaryFile.exists())
            return;

        if(dictionaryFile.listFiles() == null || dictionaryFile.listFiles().length == 0)
            return;

        for(File file : dictionaryFile.listFiles()) {
            if (file != null) {
                if (((Calendar.getInstance().getTimeInMillis() - getCalendarFromDate(file.getName()).getTimeInMillis()) / (24 * 60 * 60 * 1000)) >= Constant.LOG_FILE_SAVE_DAYS) {
                    file.delete();
                }
            }
        }
    }

    private static Calendar getCalendarFromDate(final String dateTime) {

        String date = dateTime.split("\\.")[0];

        int year = 0;
        int month = 0;
        int day = 0;

        try {
            String[] array = date.split("-");

            int[] arrayInt = new int[array.length];

            for (int i = 0; i < array.length; i++) {
                arrayInt[i] = Integer.parseInt(array[i]);
                if(i == 0) {
                    year = arrayInt[0];
                } else if(i == 1){
                    month = arrayInt[1];
                } else if(i == 2){
                    day = arrayInt[2];
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        if(year > 0 && month >= 0 && day >= 0) {
            cal.set(year, month - 1, day , 0 , 0 , 0);
        }
        return cal;
    }
}
